<template>
  <div class="login_container">
    <div class="login_box">
      <h2>疫苗接种系统用户端</h2>
      <!-- 登录表单 -->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        class="login_form"
        :rules="rules"
      >
        <!-- 用户名 -->
        <el-form-item prop="userId">
          <el-input
            v-model="loginForm.userId"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-wodemima"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="text" @click="onregister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
// import register from "./register.vue";

export default {
  components: {
    // register
  },
  data() {
    return {
      // 表单数据
      loginForm: {
        userId: "",
        password: ""
      },
      rules: {
        userId: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    // 重置表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    // 登录
    login() {
      this.$refs.loginFormRef.validate(async value => {
        if (!value) return;

        const { data: res } = await this.$http.post(
          "http://146.56.227.53:8763/user/login",
          this.loginForm
        );
        if (res.code == 200) {
          let objStr = JSON.stringify(res.data);
          this.$message.success(res.msg);
          this.$router.push("/Home");
          sessionStorage.setItem("userData", objStr);
        } else {
          this.$message.error(res.msg);
        }
        console.log(res);
      });
    },
    isShow() {
      this.visible = false;
    },
    onregister() {
      this.$router.push("/register");
    }
  }
};
</script>

<style lang="less">
.login_container {
  height: 100%;
  background-color: #0b3660;
}

.login_box {
  height: 300px;
  width: 450px;
  border-radius: 5px;
  background-color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btn {
  display: flex;
  justify-content: center;
}
</style>
